package com.nervenets.general.web.params;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nervenets.general.annotation.IgnoreSwaggerParameter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.*;
import java.util.List;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public abstract class UpdateParams<T> extends QueryParams<T> {
    @IgnoreSwaggerParameter
    @JsonIgnore
    @JSONField(serialize = false)
    protected CriteriaUpdate<T> update;

    public void generateUpdateSpecification(List<Predicate> predicates, Root<T> root, CriteriaUpdate<T> update, CriteriaQuery<T> query, CriteriaBuilder builder) {
        this.update = update;
        this.generateCustomSpecification(predicates, root, query, builder);
    }

    public void addFieldSet(String field, Object value) {
        if (null != field && null != value) {
            this.update.set(root.get(field), value);
        }
    }
}
